from django.shortcuts import render, redirect
from Organization.models import *
from User.models import *
# Create your views here.
'''
    deptHome  ：科室主页
    deptCreate：增加科室
    deptRemove：删除科室
    deptUpdate：更新科室
    deptQuery： 查询科室
'''
def deptHome(request):
    L_deptList = Department.departments.all()
    context = {'L_deptList': L_deptList}
    return render(request, 'Organization/department.html', context)
def deptCreate(request):
    dept = request.POST.get('L_deptCreate')
    Department.departments.create(dept)
    return redirect('../deptHome/')
def deptRemove(request, id):
    Department.departments.remove(id)
    return redirect('../deptHome/')
def deptUpdate(request, id):
    dept = request.POST.get('L_deptUpdate')
    Department.departments.update(id, dept)
    return redirect('../deptHome/')
def deptQuery(request):
    id = request.POST.get('L_deptQuery')
    L_deptList = Department.departments.query(id)
    context = {'L_deptList': L_deptList}
    return render(request, 'Organization/department.html', context)
'''
    posHome  ：职务主页
    posCreate：增加职务
    posRemove：删除职务
    posUpdate：更新职务
    posQuery ：查询职务
'''
def posHome(request):
    L_posList = Position.positions.all()
    context = {'L_posList': L_posList}
    return render(request, 'Organization/position.html', context)
def posCreate(request):
    pos = request.POST.get('L_posCreate')
    Position.positions.create(pos)
    return redirect('../posHome/')
def posRemove(request, id):
    Position.positions.remove(id)
    return redirect('../posHome/')
def posUpdate(request, id):
    pos = request.POST.get('L_posUpdate')
    Position.positions.update(id, pos)
    return redirect('../posHome/')
def posQuery(request):
    id = request.POST.get('L_posQuery')
    L_posList = Position.positions.query(id)
    context = {'L_posList': L_posList}
    return render(request, 'Organization/position.html', context)
'''
    staffHome  ：员工主页
    staffCreate：增加员工 
    staffRemove：删除员工
    staffUpdate：更新员工
    staffQuery： 查询员工
'''
def staffHome(request):
    L_department = Department.departments.all()
    L_position = Position.positions.all()
    L_staffList = Staff.staffs.all().filter(isDelete = False).values('name', 'job', 'gender', 'positions__posName','departments__deptName','id')
    context = {'L_staffList': L_staffList, 'L_department': L_department, 'L_position': L_position}
    return render(request, 'Organization/staff.html', context)
def staffCreate(request):
    job = request.POST.get('L_jobCreate')
    name = request.POST.get('L_nameCreate')
    gender = request.POST.get('L_genderCreate')
    departments = request.POST.get('L_departmentsCreate')
    positions = request.POST.get('L_positionsCreate')
    Staff.staffs.create(job, name, gender, departments, positions)
    return redirect('../staffHome/')
def staffRemove(request, id):
    Staff.staffs.remove(id)
    return redirect('../staffHome/')
def staffUpdate(request):
    id = request.POST.get('L_idUpdate')
    job = request.POST.get('L_jobUpdate')
    name = request.POST.get('L_nameUpdate')
    gender = request.POST.get('L_genderUpdate')
    departments = request.POST.get('L_departmentsUpdate')
    positions = request.POST.get('L_positionsUpdate')
    Staff.staffs.update(id, job, name, gender, departments, positions)
    return redirect('../staffHome/')
def staffQuery(request):
    id = request.POST.get('L_staffQuery')
    L_staffList = Staff.staffs.query(id)
    L_department = Department.departments.all()
    L_position = Position.positions.all()
    context = {'L_staffList': L_staffList, 'L_department': L_department, 'L_position': L_position}
    return render(request, 'Organization/staff.html', context)
'''
    userHome  ：用户主页
    userRemove：删除用户
    userUpdate：更新用户
    userQuery： 查询用户
'''
def userHome(request):
    L_userList = User.users.all().filter(isDelete = False)
    context = {'L_userList':L_userList}
    return render(request, 'Organization/user.html', context)
def userRemove(request, id):
    User.users.remove(id)
    return redirect('../userHome/')
def userUpdate(request, id):
    role = request.POST.get('L_userUpdate')
    User.users.update(id, role)
    return redirect('../userHome')
def userQuery(request):
    id = request.POST.get('L_userQuery')
    L_userList = User.users.query(id)
    context = {'L_userList': L_userList}
    return render(request, 'Organization/user.html', context)
'''
    login   ：用户登录
    register：用户注册
    base    ：OA主页
'''
def login(request):
    return render(request, 'User/login.html')
def register(request):
    L_staffList = Staff.staffs.all()
    content = {'L_staffList': L_staffList}
    return render(request, 'User/register.html', content)
def base(request):
    return render(request, 'base.html')
